const { Op } = require('sequelize');

const seq = require('../db/seq');
const Paster = require('../model/paster');
const User = require('../model/user');
const User_paster = require('../model/user_paster');

const paster = Paster(seq);
const user_paster = User_paster(seq);
const user = User(seq);

class PasterController {
    //获取积分商城贴纸
    async getAll(ctx, next) {
        try {
            const { count, rows } = await paster.findAndCountAll({
                where: {
                    paster_integral: {
                        [Op.ne]: 0
                    }
                }
            })
            ctx.body = {
                code: 0,
                message: '获取积分商城贴纸成功',
                result: {
                    count: count,
                    data: rows
                }
            }
        } catch (err) {
            console.log('获取积分商城贴纸失败', err);
            return ctx.body = {
                error: '获取积分商城贴纸失败'
            }
        }
    }

    //获取用户贴纸
    async getPaster(ctx, next) {
        const user_id = ctx.state.user.user_id;
        const { pasterType } = ctx.request.params;
        try {
            const { count, rows } = await user_paster.findAndCountAll({
                include: {
                    model: paster,
                    as: 'paster_info',
                    attributes: ['paster_integral', 'paster_src']
                },
                where: {
                    [Op.or]: [{ user_id: user_id }, { user_id: 'default_id' }],
                    //     user_id: 'default_id',
                    paster_type: pasterType
                }
            })
            ctx.body = {
                code: 0,
                message: '获取用户贴纸成功',
                result: {
                    count: count,
                    data: rows
                }
            }
        } catch (err) {
            console.log('获取用户贴纸失败', err);
            return ctx.body = {
                error: '获取用户贴纸失败'
            }
        }

    }

    //兑换贴纸
    async exchange(ctx, next) {
        const user_id = ctx.state.user.user_id;
        const { paster_id } = ctx.request.body;
        try {
            //查询该贴纸的类型和所需积分
            const res1 = await paster.findOne({ where: paster_id });
            const { paster_integral, paster_type } = res1.dataValues;
            if ((paster_type != 0) && (paster_type != 1)) {
                return ctx.body = {
                    error: '错误的paster_id'
                }
            }
            //在用户的贴纸表中新增一条数据
            const res2 = await user_paster.create({ user_id, paster_id, paster_type });
            //获取当前用户账户里的积分
            const res3 = await user.findOne({ attributes: ['user_integral'], where: { user_id } });
            const { user_integral } = res3.dataValues;
            //更改当前用户账户中的积分数
            const newIntegral = user_integral - paster_integral;
            if (newIntegral > 0) {
                const res4 = await user.update({ user_integral: newIntegral }, { where: { user_id } });
                ctx.body = {
                    code: 0,
                    message: '兑换贴纸成功'
                }
            } else {
                return ctx.body = {
                    error: '用户积分不足'
                }
            }
        } catch (err) {
            console.log('兑换贴纸失败', err);
            return ctx.body = {
                error: '兑换贴纸失败'
            }
        }
    }

    //获取用户积分
    async getIntegral(ctx, next) {
        const user_id = ctx.state.user.user_id;
        try {
            const res = await user.findOne({ attributes: ['user_integral'], where: { user_id } });
            ctx.body = {
                code: 0,
                message: '查询用户积分成功',
                result: res.dataValues
            }
        } catch (err) {
            console.log('获取用户积分失败', err);
            return ctx.body = {
                error: '获取用户积分失败'
            }
        }

    }

    //获取茶具信息
    async getTeacup(ctx, next) {
        try {
            const { count, rows } = await paster.findAndCountAll({ where: { paster_type: 2 } });
            ctx.body = {
                code: 0,
                message: '获取茶具信息成功',
                result: {
                    count: count,
                    data: rows
                }
            }
        } catch (err) {
            console.log('获取茶具信息失败', err);
            return ctx.body = {
                error: '获取茶具信息失败'
            }
        }

    }
}

module.exports = new PasterController();